package com.hancher.common.javautil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/7/21 22:03 <br/>
 * 描述 : 时间工具纯静态类
 */
public class DateUtil {
    public static final long ONE_YEAR = 365L * 24 * 60 * 60 * 1000;
    public static final long ONE_DAY = 24L * 60 * 60 * 1000;
    public static final long ONE_HOUR = 60L * 60 * 1000;

    /**
     * stamp -> yyyy-MM-dd HH:mm:ss
     *
     * @param time
     * @return 时间
     */
    public static String stamp2allStr(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.format(new Date(time));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * stamp -> yyyy-MM-dd HH:mm:ss
     *
     * @param time
     * @return 时间
     */
    public static String stamp2MdHm(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        try {
            return simpleDateFormat.format(new Date(time));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * stamp -> HH
     *
     * @param time
     * @return 时间
     */
    public static String stamp2HStr(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        try {
            return simpleDateFormat.format(new Date(time));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * stamp -> HH
     *
     * @param time
     * @return 时间
     */
    public static String stamp2HmStr(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        try {
            return simpleDateFormat.format(new Date(time));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * stamp -> dd
     *
     * @param time
     * @return 时间
     */
    public static String stamp2DStr(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        try {
            return simpleDateFormat.format(new Date(time));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * stamp -> yyyy-MM-dd
     *
     * @param time
     * @return 时间
     */
    public static String stamp2YmdStr(Long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.format(new Date(time));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * HHmm -> stamp
     *
     * @param time 小时：分钟
     * @return 时间戳
     */
    public static Long hmStr2stamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);
        calendar.add(Calendar.HOUR, 8);
        return date.getTime() + calendar.getTimeInMillis();
    }

    /**
     * HHmm -> stamp
     *
     * @param time 小时：分钟
     * @return 时间戳
     */
    public static Long allstr2stamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            return 0L;
        }
    }

    /**
     * HH -> stamp
     *
     * @param timeH 小时
     * @return 时间戳
     */
    public static Long hStr2stamp(String timeH, boolean isNextDay) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), Integer.parseInt(timeH), 0, 0);
        } catch (Exception e) {
            return null;
        }
        if (isNextDay) {
            calendar.add(Calendar.DATE, 1);
        }

        return calendar.getTimeInMillis();
    }

    /**
     * ddHH -> stamp
     *
     * @param timeH 小时
     * @return 时间戳
     */
    public static String hh2ddhh(String timeH, int addDay) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), Integer.parseInt(timeH), 0, 0);
        } catch (Exception e) {
            return null;
        }
        if (addDay != 0) {
            calendar.add(Calendar.DATE, addDay);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddHH");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * +1 -> MMdd
     *
     * @param addDay
     * @return
     */
    public static String getRelativeMMdd(int addDay) {
        Calendar calendar = Calendar.getInstance();
        if (addDay != 0) {
            calendar.add(Calendar.DATE, addDay);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMdd");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * stamp -> 是夜晚
     *
     * @param measureNowTime
     * @return
     */
    public static boolean isNight(Long measureNowTime) {
        if (measureNowTime == null) {
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(measureNowTime);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour < 6 || hour > 18;
    }

    /**
     * MMdd -> stamp
     *
     * @param time
     * @return
     */
    public static long changeMMdd2Stamp(String time) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.set(calendar.get(Calendar.YEAR), Integer.parseInt(time.substring(0, 2)) - 1,
                    Integer.parseInt(time.substring(2)), 0, 0, 0);
        } catch (Exception e) {

            return 0;
        }
        return calendar.getTimeInMillis();
    }

    /**
     * ddHH -> yyyy-MM-dd hh:mm:ss
     *
     * @param predictTime
     * @return
     */
    public static String changeMMdd2str(String predictTime) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.set(calendar.get(Calendar.YEAR), Integer.parseInt(predictTime.substring(0, 2)) - 1,
                    Integer.parseInt(predictTime.substring(2)), 0, 0, 0);
//            calendar.set(Calendar.AM_PM, Calendar.AM_PM);
        } catch (Exception e) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * ddHH -> yyyy-MM-dd hh:mm:ss
     *
     * @param predictTime
     * @return
     */
    public static String changeddHH2str(String predictTime) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), Integer.parseInt(predictTime.substring(0, 2)),
                    Integer.parseInt(predictTime.substring(2)), 0, 0);
        } catch (Exception e) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取 yyyyMMdd_HHmmss
     *
     * @return yyyyMMdd_HHmmss
     */
    public static String getAllStrPath() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取 yyyyMMdd_HHmmss
     *
     * @return yyyyMMdd_HHmmss
     */
    public static String getMdHmStrPath() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        return simpleDateFormat.format(new Date());
    }

    public static String getAllstr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取 yyyy-MM
     *
     * @return yyyy-MM
     */
    public static String getYearMonthstr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取 yyyy-MM
     *
     * @return yyyy-MM
     */
    public static String getYearMonthstr2() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 今天第一毫秒时间
     *
     * @return
     */
    public static Long getFirstStampOfToday() {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE), 0, 0, 0);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 今天第一毫秒时间
     *
     * @return
     */
    public static Long getFirstStampOfToday(int addDay) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE), 0, 0, 0);
            calendar.add(Calendar.DATE, addDay);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 选定日期增加后的时间戳
     *
     * @return
     */
    public static Long getFirstStampOfDay(Date date, int addDay) {
        try {
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(date);
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH),
                    calendar2.get(Calendar.DATE), 0, 0, 0);
            calendar.add(Calendar.DATE, addDay);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 选定日期增加后的时间戳
     *
     * @return
     */
    public static Long getFirstStampOfDay(int year, int month, int day, int addDay) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month - 1, day, 0, 0, 0);
            calendar.add(Calendar.DATE, addDay);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 今天第一毫秒时间
     *
     * @return
     */
    public static Date getFirstStampOfTodayDate(int addDay) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE), 0, 0, 0);
            calendar.add(Calendar.DATE, addDay);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 获取加减年后的日期
     *
     * @return
     */
    public static Date getDateAddYear(int addYear) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE), 0, 0, 0);
            calendar.add(Calendar.YEAR, addYear);
            return calendar.getTime();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 选定日期增加后的时间戳
     *
     * @return
     */
    public static Long getFirstStampOfMonth(Date date, int addMonth) {
        try {
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(date);
            calendar2.add(Calendar.MONTH, addMonth);

            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH),
                    1, 0, 0, 0);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 获取指定月份第一针时间戳
     *
     * @param year
     * @param month    1-12
     * @param addMonth
     * @return
     */
    public static Long getFirstStampOfMonth(int year, int month, int addMonth) {
        try {
            Calendar calendar2 = Calendar.getInstance();
            calendar2.set(year, month - 1, 1, 0, 0, 0);
            calendar2.add(Calendar.MONTH, addMonth);

            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH),
                    1, 0, 0, 0);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 是否是同一天
     *
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isSameDay(Long time1, long time2) {
        return stamp2YmdStr(time1).equals(stamp2YmdStr(time2));
    }

    public static String changeDate2Ymd(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.format(date);
        } catch (Exception e) {

            return "";
        }
    }

    public static String changeStamp2Ymd(Long stamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.format(new Date(stamp));
        } catch (Exception e) {

            return "";
        }
    }

    public static String changeDate2Ym(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        try {
            return simpleDateFormat.format(date);
        } catch (Exception e) {

            return "";
        }
    }

    /**
     * 获取月第一毫秒
     *
     * @param date     选中日期
     * @param addMonth 增加月份
     * @return
     */
    public static Long getStampOfMonth(Date date, int addMonth) {
        try {
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(date);
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH),
                    calendar2.get(Calendar.DATE), 0, 0, 0);
            calendar.add(Calendar.MONTH, addMonth);
            return calendar.getTimeInMillis();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 获取月份天数
     *
     * @param date 选中日期
     * @return
     */
    public static int getDayCountOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static long getDiffDay(Long stamp1, Long stamp2) {
        return (stamp1 - stamp2) / ONE_DAY;
    }

    public static long getDiffYear(Long stamp1, Long stamp2) {
        return (stamp1 - stamp2) / ONE_YEAR;
    }

    public static String ymdhms2Allstr(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, hour, minute, second);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(calendar.getTime());
    }

    public static Long ymdhms2stamp(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, hour, minute, second);
        return calendar.getTimeInMillis();
    }

    public static String ymdhms2week(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, hour, minute, second);
        return stamp2week(calendar.getTimeInMillis());
    }
    public static String stamp2week(Long stamp) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(stamp);
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            case 7:
                return "周六";
            default:
                return "";
        }
    }

    public static int getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 返回月份1-12
     * @return
     */
    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    public static int getYear(long stamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(stamp);
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(long stamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(stamp);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDay(long stamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(stamp);
        return calendar.get(Calendar.DATE);
    }
}
